package com.mezz.bones.mcp.server.config;

import com.mezz.bones.mcp.server.annotation.McpTool;
import jakarta.annotation.Resource;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
@Configuration
public class McpConfig {

    @Resource
    private ApplicationContext applicationContext;

    @Bean
    public ToolCallbackProvider mcpTools() {

        // 获取所有带有@Tool注解方法的Bean
        Map<String, Object> toolBeans = applicationContext.getBeansWithAnnotation(McpTool.class);
        
        log.info("发现MCP工具服务: {}", toolBeans.keySet());
        
        // 构建 ToolCallbackProvider
        MethodToolCallbackProvider.Builder builder = MethodToolCallbackProvider.builder();
        
        // 添加所有工具
        builder.toolObjects(toolBeans.values().toArray());
        
        return builder.build();
    }
}
